﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Data;
using DAL.EstRec;

namespace BAL.EstRec.HQConsolidacion
{
    public class Consolidacion
    {
        private Pos _pos;
        private Recurso _recurso;
        private int _confirmed;

        public Pos Pos {
            get { return _pos; }
            set { _pos=value; } 
        }
        public Recurso Recurso 
        {
            get { return _recurso; }
            set { _recurso = value; }
        }
        public int Confirmed
        {
            get { return _confirmed; }
            set { _confirmed = value; }
        }

        public Consolidacion(Pos p, Recurso r, int confirmed)
        {
            this._pos = p;
            this._recurso = r;
            this._confirmed = confirmed;
        }

        public static ObservableCollection<Consolidacion> GetConsolidacion(long unidVersion)
        {
            DataSet res;
            ObservableCollection<Consolidacion> oc = new ObservableCollection<Consolidacion>();
            SqlCommand sc = new SqlCommand();
            sc.CommandType = CommandType.StoredProcedure;
            sc.CommandText = "SP_GET_CONSOLIDACION_BY_UNID";
            sc.Parameters.Add(new SqlParameter("@unidVersion", unidVersion));

            try
            {
                res = SrvDB.ExecuteCommand(sc);
                if (res.Tables[0].Rows.Count > 0)
                {
                    oc = new ObservableCollection<Consolidacion>();
                    res.Tables[0].AsEnumerable().ToList().ForEach(
                        row =>
                        {
                            oc.Add(new Consolidacion
                                (
                            new Pos(
                                    row.IsNull("ID_POS") ? "" : row["ID_POS"].ToString()
                                    , row.IsNull("POS") ? "" : row["POS"].ToString()
                                    )
                            , new Recurso(
                                    row.IsNull("ID_RECURSO") ? "" : row["ID_RECURSO"].ToString()
                                    , row.IsNull("RECURSO") ? "" : row["RECURSO"].ToString()
                                    )
                            , row.IsNull("CONFIRMED") ? 0 : (int)row["CONFIRMED"]
                                ));
                        }
                    );
                }

            }
            catch (Exception ex)
            {
                throw new Exception("Error al obtener información de la base. Valide su conexión a la base y los permisos e intentelo nuevamente.",ex);
            }

            return oc;
        }
    }
}
